package com.xckj.cycling.view

import android.animation.ValueAnimator
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import androidx.appcompat.content.res.AppCompatResources
import androidx.lifecycle.LifecycleOwner
import com.mapbox.android.gestures.MoveGestureDetector
import com.mapbox.geojson.Point
import com.mapbox.maps.CameraOptions
import com.mapbox.maps.Style
import com.mapbox.maps.extension.style.expressions.dsl.generated.interpolate
import com.mapbox.maps.plugin.LocationPuck2D
import com.mapbox.maps.plugin.gestures.OnMoveListener
import com.mapbox.maps.plugin.gestures.gestures
import com.mapbox.maps.plugin.locationcomponent.*
import com.xckj.cycling.R
import com.yalantis.ucrop.util.EglUtils

open class CMapView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : LifecycleMapView(context, attrs) {
    override fun onCreate(owner: LifecycleOwner) {
        super.onCreate(owner)
        onMapReady()
    }

    open fun onMapReady() {
        getMapboxMap().setCamera(
            CameraOptions.Builder()
                .zoom(12.0)
                .build()
        )
        getMapboxMap().loadStyleUri(
            Style.MAPBOX_STREETS
        ) {
            initLocationComponent()
            setupGesturesListener()
            onStyleLoad(it)
        }
    }

    protected open fun onStyleLoad(style: Style) {
        setupGesturesListener()
    }

    fun resetStyle() {
    }

    var lastPoint: Point? = null
    var lastBearing: Double? = 0.0
    private fun initLocationComponent() {
        val locationComponentPlugin = location
        locationComponentPlugin.getLocationProvider()
            ?.registerLocationConsumer(object : LocationConsumer {
                override fun onBearingUpdated(
                    vararg bearing: Double,
                    options: (ValueAnimator.() -> Unit)?
                ) {
                    lastBearing = bearing[0]
                }

                override fun onLocationUpdated(
                    vararg location: Point,
                    options: (ValueAnimator.() -> Unit)?
                ) {
                    lastPoint = location[0]
                    Log.d("onLocationUpdated", "point: " + location[0].toJson())
                }

                override fun onPuckBearingAnimatorDefaultOptionsUpdated(options: ValueAnimator.() -> Unit) {
                }

                override fun onPuckLocationAnimatorDefaultOptionsUpdated(options: ValueAnimator.() -> Unit) {
                }

            })

        updateSetting(locationComponentPlugin)
        locationComponentPlugin.addOnIndicatorPositionChangedListener(
            onIndicatorPositionChangedListener
        )
        locationComponentPlugin.addOnIndicatorBearingChangedListener(
            onIndicatorBearingChangedListener
        )
    }

    open fun updateSetting(
        locationComponentPlugin: LocationComponentPlugin,
        navigation: Boolean = false
    ) {
        locationComponentPlugin.updateSettings {
            this.enabled = true
            this.locationPuck = LocationPuck2D(
                bearingImage = AppCompatResources.getDrawable(
                    context,
                    if (navigation) com.mapbox.navigation.ui.maps.R.drawable.mapbox_navigation_puck_icon else R.mipmap.mapbox_mylocation_icon_default
//
                ),
                shadowImage = AppCompatResources.getDrawable(
                    requireContext(),
                    com.mapbox.maps.R.drawable.mapbox_user_icon_shadow
//                    R.drawable.mapbox_user_icon_shadow,
                ),
                scaleExpression = interpolate {
                    linear()
                    zoom()
                    stop {
                        literal(0.0)
                        literal(0.6)
                    }
                    stop {
                        literal(20.0)
                        literal(1.0)
                    }
                }.toJson()
            )
        }
    }

    var captureing = false //是否在截图
    private val onIndicatorBearingChangedListener = OnIndicatorBearingChangedListener {
        if (captureing) {
            return@OnIndicatorBearingChangedListener
        }
        getMapboxMap().setCamera(CameraOptions.Builder().bearing(it).build())
    }

    private val onIndicatorPositionChangedListener = OnIndicatorPositionChangedListener {
        lastPoint = it
        if (captureing) {
            return@OnIndicatorPositionChangedListener
        }
        getMapboxMap().setCamera(CameraOptions.Builder().center(it).build())
        gestures.focalPoint = getMapboxMap().pixelForCoordinate(it)
    }
    private val onMoveListener = object : OnMoveListener {
        override fun onMoveBegin(detector: MoveGestureDetector) {
            onCameraTrackingDismissed()
        }

        override fun onMove(detector: MoveGestureDetector): Boolean {
            return false
        }

        override fun onMoveEnd(detector: MoveGestureDetector) {}
    }

    private fun onCameraTrackingDismissed() {
    }


    fun setupGesturesListener() {
        gestures.addOnMoveListener(onMoveListener)
    }


    override fun onDestroy() {
        super.onDestroy()
        location
            .removeOnIndicatorBearingChangedListener(onIndicatorBearingChangedListener)
        location
            .removeOnIndicatorPositionChangedListener(onIndicatorPositionChangedListener)
        gestures.removeOnMoveListener(onMoveListener)
    }

}